package com.xgkj.diyiketang.util;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.R;

/**
 * Created by windo on 16/5/16.
 */
public class NoDataUtil {
    private Context mContext;

    private RelativeLayout rootLayout;
    //网络连接失败的layout
    private LinearLayout errorLayout;
    // 进度圈
    private ProgressBar mProgressBar;
    //网络连接失败的TextView
    private TextView errorTextView;

    //内容为空的layout
    private LinearLayout emptyLayout;

    //内容为空的图片
    private ImageView emptyImageView;

    private TextView emptyTextView;

    private TextView secondTextView;


    public NoDataUtil(Context context, View v) {
        mContext = context;
        rootLayout = (RelativeLayout) v.findViewById(R.id.content_layout);
        errorLayout = (LinearLayout) v.findViewById(R.id.content_error_empty);
        mProgressBar = (ProgressBar) v.findViewById(R.id.empty_bar);
        errorTextView = (TextView) v.findViewById(R.id.empty_content);

        emptyLayout = (LinearLayout) v.findViewById(R.id.empty_content_view);
        emptyImageView = (ImageView) v.findViewById(R.id.empty_img);
        emptyTextView = (TextView) v.findViewById(R.id.text_empty);
        secondTextView = (TextView) v.findViewById(R.id.text_empty_down);
    }

    public void ListViewError(ListView mListView, final View.OnClickListener listener) {
        rootLayout.setVisibility(View.VISIBLE);
        errorLayout.setVisibility(View.VISIBLE);
        emptyLayout.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);
        errorTextView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
        SpannableString ss = new SpannableString(
                mContext.getText(R.string.no_net_to_refresh));
        ss.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.hint_color)), 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(
                new ForegroundColorSpan(mContext.getResources().getColor(
                        R.color.nfc_open)), 8,
                mContext.getText(R.string.no_net_to_refresh).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), 8,
                mContext.getText(R.string.no_net_to_refresh).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        errorTextView.setText(ss);
        errorTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                errorTextView
                        .setText(R.string.pull_to_refresh_refreshing_label);
                mProgressBar.setVisibility(View.VISIBLE);
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
    }


    public void ListViewNoEmpty(ListView mListView) {
        mListView.setVisibility(View.VISIBLE);
        emptyLayout.setVisibility(View.GONE);
        errorLayout.setVisibility(View.GONE);
    }

    public void ListViewEmpty(ListView mListView, String firstText, String secondText, int drawableId) {
        rootLayout.setVisibility(View.VISIBLE);
        emptyLayout.setVisibility(View.VISIBLE);
        errorLayout.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(firstText)) {
            emptyTextView.setVisibility(View.VISIBLE);
            emptyTextView.setText(firstText);
        } else {
            emptyTextView.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(secondText)) {
            secondTextView.setVisibility(View.VISIBLE);
            secondTextView.setText(secondText);
        } else {
            secondTextView.setVisibility(View.GONE);
        }

        if (drawableId != -1) {
            emptyImageView.setImageResource(drawableId);
        }
    }

    public void GridViewNoEmpty(PullToRefreshGridView mListView) {
        mListView.setVisibility(View.VISIBLE);
        emptyLayout.setVisibility(View.GONE);
        errorLayout.setVisibility(View.GONE);
    }

    public void GridViewEmpty(PullToRefreshGridView mListView, String firstText, String secondText, int drawableId) {
        emptyLayout.setVisibility(View.VISIBLE);
        errorLayout.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(firstText)) {
            emptyTextView.setVisibility(View.VISIBLE);
            emptyTextView.setText(firstText);
        } else {
            emptyTextView.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(secondText)) {
            secondTextView.setVisibility(View.VISIBLE);
            secondTextView.setText(secondText);
        } else {
            secondTextView.setVisibility(View.GONE);
        }

        if (drawableId != -1) {
            emptyImageView.setImageResource(drawableId);
        }
    }
}
